package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ProcessContent;
import com.isunland.managesystem.entity.WorkProcessDetailList.ProcessRegistDetailContent;

/**
 * 工作计划登记详情
 *
 * @author wu
 */

public class WorkProcessDetailFragment extends Fragment {

  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.extra_value";
  public static final String EXTRA_TASK = "com.isunland.managesystem.ui.extra_task";
  private TextView mIfFinishPlanDetail; // 是否完成
  private TextView mPutFinishTimePlanDetail;// 本阶段完成时间
  private TextView mPutFinishPercentPlanDetail;// 完成度
  private TextView mPutUseTimePlanDetail;// 所用工时
  private TextView mePutFinishConditionPlanDetail;// 完成情况
  private TextView mPlanContentPlanDetail;// 任务内容
  private TextView mAppointerPlanDetail;// 委派人
  private TextView mRequestFinishTimePlanDetail;// 要求 完成时间
  //private TextView mRegisterPlanDetail;// 登记人
  //private TextView mRegistTimePlanDetail;// 登记时间

  private String misFinish; // 是否完成
  private String mCompleteTime;// 本阶段完成时间
  private String mCompletePercent;// 完成度
  private String mUseTime;// 所用工时
  private String mCompleteCondition;// 完成情况
  private String mContent;// 任务内容
  private String mApponiter;// 委派人
  private String mRequestTime;// 要求 完成时间
  //private String mRegister;// 登记人
  //private String mRegistTime;// 登记时间
  private ProcessContent mTask;//任务详情
  private TextView mTvExecutor;//执行人
  private TextView mTvClasses;//计划任务类别

  public static Fragment newInstance(ProcessRegistDetailContent detail, ProcessContent task) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, detail);
    bundle.putSerializable(EXTRA_TASK, task);
    WorkProcessDetailFragment fragment = new WorkProcessDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ActionBar actionBar = ((BaseVolleyActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {

      if (NavUtils.getParentActivityName(getActivity()) != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
      }

      actionBar.setTitle(R.string.planDetail);
    }
    ProcessRegistDetailContent mPlanContent =
        (ProcessRegistDetailContent) getArguments().getSerializable(EXTRA_VALUE);
    mTask = (ProcessContent) getArguments().getSerializable(EXTRA_TASK);
    if (mPlanContent != null) {
      misFinish = mPlanContent.getIfEnd();
      mCompleteTime = mPlanContent.getCompleteDate();
      mCompletePercent = mPlanContent.getRateCurrent();
      mUseTime = mPlanContent.getWorkTimeNum();
      mCompleteCondition = mPlanContent.getExcDesc();
      mContent = mPlanContent.getPlanContentDesc();
      mRequestTime = mPlanContent.getPlanEsteTime();
      //mRegister = mPlanContent.getRegStaffName();
      //mRegistTime = mPlanContent.getRegDate();
      mApponiter = mPlanContent.getTaskManName();
    }

    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_workprocess_regist_detail, container, false);
    mIfFinishPlanDetail = (TextView) view.findViewById(R.id.tv_ifFinish_planDetail);
    mPutFinishTimePlanDetail = (TextView) view.findViewById(R.id.et_putFinishTime_planDetail);
    mPutFinishPercentPlanDetail = (TextView) view.findViewById(R.id.et_putFinishPercent_planDetail);
    mPutUseTimePlanDetail = (TextView) view.findViewById(R.id.et_putUseTime_planDetail);
    mePutFinishConditionPlanDetail =
        (TextView) view.findViewById(R.id.et_putFinishCondition_planDetail);
    mPlanContentPlanDetail = (TextView) view.findViewById(R.id.tv_planContent_planDetail);
    mAppointerPlanDetail = (TextView) view.findViewById(R.id.tv_appointer_planDetail);
    mRequestFinishTimePlanDetail =
        (TextView) view.findViewById(R.id.tv_requestFinishTime_planDetail);
    //mRegisterPlanDetail = (TextView) view.findViewById(R.id.tv_register_planDetail);
    //mRegistTimePlanDetail = (TextView) view.findViewById(R.id.tv_registTime_planDetail);
    mTvExecutor = (TextView) view.findViewById(R.id.tv_executor_planDetail);
    mTvClasses = (TextView) view.findViewById(R.id.tv_classes_planDetail);
    initData();
    return view;
  }

  private void initData() {
    if (mTask != null) {
      mTvExecutor.setText(mTask.getExcManName());
      mTvClasses.setText(mTask.getPlanKindName());
    }
    mIfFinishPlanDetail.setText(misFinish);
    mPutFinishTimePlanDetail.setText(mCompleteTime);
    mPutFinishPercentPlanDetail.setText(mCompletePercent);
    if (!TextUtils.isEmpty(mUseTime)) {
      mPutUseTimePlanDetail.setText(mUseTime);
    } else {
      mPutUseTimePlanDetail.setText(0);
    }
    mePutFinishConditionPlanDetail.setText(mCompleteCondition);

    mAppointerPlanDetail.setText(mApponiter);
    //mRegisterPlanDetail.setText(mRegister);
    //mRegistTimePlanDetail.setText(mRegistTime);
    mPlanContentPlanDetail.setText(mContent);
    mRequestFinishTimePlanDetail.setText(mRequestTime);
  }

  /**
   * 响应返回按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
